package com.motu.vertx.module.utility.mysql;

import com.motu.vertx.module.utility.base.ServerConfig;
import com.motu.vertx.module.utility.base.ServerTypeEnum;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.Pool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 获取游戏内数据库连接池对象
 */
public class MysqlClient {

	private static final Logger logger = LoggerFactory.getLogger("MysqlClient");

    /**
     * 初始化数据库连接池（调用这个方法前提是需要先初始化服务器配置信息）
     * @param vertx
     */
    public static void init(Vertx vertx){
        init(vertx, ServerConfig.getAppConfig());
    }

    /**
     * 初始化数据库连接池
     * @param vertx
     * @param appConfig 数据库配置信息
     */
    public static void init(Vertx vertx, JsonObject appConfig){
        Set<String> dbConfigSet = null;

        // 登录服数据库连接
        if (ServerConfig.isLoginServer()){
            dbConfigSet = Set.of(MysqlConstant.GAME, MysqlConstant.SERVER, MysqlConstant.COMMON, MysqlConstant.BADWORD, MysqlConstant.PROJECT);
        }
        // 网关服数据库连接
        else if (ServerConfig.isGateServer()){
            dbConfigSet = Set.of(MysqlConstant.GAME, MysqlConstant.SERVER, MysqlConstant.COMMON, MysqlConstant.PROJECT);
        }

        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.GAME)){
            gamePool = MysqlTool.createMySQLPool(vertx, appConfig, MysqlConstant.GAME);
        }
        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.SERVER)){
            serverPool = MysqlTool.createMySQLPool(vertx, appConfig, MysqlConstant.SERVER);
        }
        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.COMMON)){
            commonPool = MysqlTool.createMySQLPool(vertx, appConfig,  MysqlConstant.COMMON);
        }
        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.BADWORD)){
            badwordPool = MysqlTool.createMySQLPool(vertx, appConfig, MysqlConstant.BADWORD);
        }
        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.PROJECT)){
            projectPool = MysqlTool.createMySQLPool(vertx, appConfig, MysqlConstant.PROJECT);
        }
        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.WEBSITES)){
            websitesPool = MysqlTool.createMySQLPool(vertx, appConfig, MysqlConstant.WEBSITES);
        }
        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.MOTUSDK)){
            motuSDKPool = MysqlTool.createMySQLPool(vertx, appConfig, MysqlConstant.MOTUSDK);
        }
        if (dbConfigSet == null || dbConfigSet.contains(MysqlConstant.WEB)){
            webPool = MysqlTool.createMySQLPool(vertx, appConfig, MysqlConstant.WEB);
        }

		logger.info("MysqlClient init success.");
    }

    private static Pool gamePool;//游戏库
    private static Pool serverPool;//服务器库
    private static Pool commonPool;//公共库
    private static Pool badwordPool;//屏蔽词库
    private static Pool projectPool; //项目公共库
    private static Pool websitesPool; //连接FAQ和人工客服库
    private static Pool motuSDKPool; //魔兔SDK库
    private static Pool webPool;//OA_WEB后台库

    public static Pool getGamePool() {
        return gamePool;
    }

    public static Pool getServerPool() {
        return serverPool;
    }

    public static Pool getCommonPool() {
        return commonPool;
    }

    public static Pool getBadwordPool() {
        return badwordPool;
    }

    public static Pool getProjectPool() {
        return projectPool;
    }

    public static Pool getWebsitesPool() {
        return websitesPool;
    }

    public static Pool getMotuSDKPool() {
        return motuSDKPool;
    }

    public static Pool getWebPool() {
        return webPool;
    }
}
